<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * 行为模型
 */

class AuthTeamModel extends BaseModel {

    /* 自动验证规则 */
    protected $_validate = array(
        array('team_name', 'require', '小组名称不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('team_name', '1,20', '小组名称长度不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
        array('department_id', 'require', '您需要选择部门哦', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    );

    /* 自动完成规则 */
    protected $_auto = array(
        array('status', 1, self::MODEL_INSERT, 'string'),
    	array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
    );
    
    public function getPk(){
    	return 'team_id';
    }
    
    
    
    /**
     * @todo 将用户插入到部门中
     * 示例: 把uid=1的用户添加到group_id为1,2的部门 `AuthGroupModel->addToDepartment(1,'1,2');`
     */
    public function addToTeam($uid,$team_id,$department_id,$model='AuthTeamMember'){
    	$uid = is_array($uid)?implode(',',$uid):trim($uid,',');
    	$team_id = (int)$team_id;
    	$department_id = (int)$department_id;
    	$obj_access = M($model);
    	$uid_arr = explode(',',$uid);
    	$uid_arr = array_diff($uid_arr,array(C('USER_ADMINISTRATOR')));
    	$user_list = M($model)->field('member_id')->where(array('department_id'=>$department_id,'team_id'=>$team_id,'member_id'=>array('in',$uid_arr)))->select();
    	if(!empty($user_list)){
    		$uid_arr = array_merge(array_diff($uid_arr, array_column($user_list, 'member_id')));
    		if(empty($uid_arr) || is_null($uid_arr)){
    			return true;
    		}
    	}
    	$data_list = array();
		foreach ($uid_arr as $u){
    			//判断用户id是否合法
    		if(M('Member')->getFieldByUid($u,'uid') == false){
    			$this->error = "编号为{$u}的用户不存在！";
    			return false;
    		}
    		if(M('auth_team_member')->getFieldByMemberId($u,'member_id') != false){
    			$this->error = "编号为{$u}的用户已存在于某个小组中！";
    			return false;
    		}
    		$data_list[] = array('department_id'=>$department_id,'team_id'=>$team_id,'member_id'=>$u);
    	}
    	if(!empty($data_list)){
    		$obj_access->addAll($data_list);
    	}
    	if (!empty($data_list)) {
    		if ($obj_access->getDbError()) {
    			if( count($uid_arr)==1 && count($team_id)==1 ){
    				//单个添加时定制错误提示
    				$this->error = "不能重复添加";
    			}else{
    				$this->error = $obj_access->getDbError();
    			}
    			return false;
    		}else{
    			return true;
    		}
    	}else{
    		return true;
    	}
    }
    
    
    /**
     * @todo 将用户从部门中移除
     * @param int|string|array $team_id   部门id
     * @param int|string|array $uid   用户id
     */
    public function removeUserFromDepart($member_id,$team_id){
    	return M('AuthTeamMember')->where( array( 'member_id'=>$member_id,'team_id'=>$team_id) )->delete();
    }

}
